﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using FrameWork.DataBaseProvider;

namespace IndexService
{
    partial class RebuiltFileTimeService : ServiceBase
    {
        private Framework.Log.ILog log = Framework.Log.LogFactory.CreateInstance();

        public RebuiltFileTimeService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            try
            {
                    log.WirteLog(string.Concat(DateTime.Now.ToString("【yyyy-MM-dd hh:mm:ss】"), "归档库重建索引启动！"));

                    log.WirteLog(string.Concat("\r\n", DateTime.Now.ToString("【yyyy-MM-dd hh:mm:ss】"), "归档库重建操作开始...."));
                    log.WirteLog(string.Concat(DateTime.Now.ToString("【yyyy-MM-dd hh:mm:ss】"), " 归档库重建【重新构建xml数据开始】......"));

                    //准备创建索引的数据
                    int dataCount = IndexCore.PreapareData(EnumServices.FILE_TIME_REBUILT);

                    //if (wordApp != null)//再关闭
                    //{
                    //    System.Runtime.InteropServices.Marshal.ReleaseComObject(wordApp);
                    //    wordApp = null;
                    //}


                    log.WirteLog(string.Concat(DateTime.Now.ToString("【yyyy-MM-dd hh:mm:ss】"), " 归档库重建【构建xml数据结束】,共构建数据数：", dataCount, "条"));

                    log.WirteLog(string.Concat(DateTime.Now.ToString("【yyyy-MM-dd hh:mm:ss】"), " 归档库重建【创建索引开始】......"));

                    //创建索引，创建完成一个，update一条记录的状态
                    string updateIndexSql = "";
                    int indexCount = 0;
                    if (dataCount <= 0)
                    {
                        log.WirteLog(string.Concat(DateTime.Now.ToString("【yyyy-MM-dd hh:mm:ss】"), " 归档库重建【创建索引结束（无新增数据）】"));
                        return;
                    }

                    updateIndexSql = IndexCore.CreateIndex(ref indexCount, EnumServices.REAL_TIME_REBUILT);
                    log.WirteLog(string.Concat(DateTime.Now.ToString("【yyyy-MM-dd hh:mm:ss】"), " 归档库重建【创建索引结束】,共更新记录数：", indexCount, "条"));

                    if (updateIndexSql != "")
                    {
                        bool result = false;
                        log.WirteLog(string.Concat(DateTime.Now.ToString("【yyyy-MM-dd hh:mm:ss】"), " 归档库重建更新isXML状态为2【更新数据库开始】......"));
                        result =new DbHelper(Const.FILE_TIME_DBCONNECTION).ExeMoreDB(updateIndexSql);
                        log.WirteLog(string.Concat(DateTime.Now.ToString("【yyyy-MM-dd hh:mm:ss】"), " 归档库重建更新isXML状态为2【更新数据库结束】更新", result ? "成功" : "失败"));
                    }

                }
                catch (Exception ex)
                {
                    log.WirteLog(ex.Message);
                }
                finally
                {
                    log.WirteLog(string.Concat(DateTime.Now.ToString("【yyyy-MM-dd hh:mm:ss】"), "重建操作结束....\r\n"));
                }
            }
  
        

        protected override void OnStop()
        {
            try
            {
                log.WirteLog(string.Concat(DateTime.Now.ToString("【yyyy-MM-dd hh:mm:ss】"), "归档库重建停止！"));

            }
            catch (Exception ex)
            {
                log.WirteLog(ex.Message);
                log.WirteLog(string.Concat(DateTime.Now.ToString("【yyyy-MM-dd hh:mm:ss】"), "归档库重建被终止！"));
            }
        }
    }
}
